﻿Imports System.Reflection

Namespace Converters

    Public Class StateToStatusBarColorConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Dim root As FrameworkElement = TryCast(Application.Current.MainWindow, FrameworkElement)
                Select Case value
                    'Local
                    'Syncing
                    'Synced
                    '[Error]

                    Case 0
                        Return root.FindResource("StatusDisconnected")

                    Case 1
                        Return root.FindResource("StatusChecking")

                    Case 2
                        Return root.FindResource("StatusConnected")

                    Case 3
                        Return root.FindResource("StatusError")

                    Case Else
                        Return root.FindResource("StatusDisconnected")

                End Select

            Catch ex As Exception
                Return New SolidColorBrush(Colors.LightGray)
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class StateToStatusDescriptionConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Dim root As FrameworkElement = TryCast(Application.Current.MainWindow, FrameworkElement)
                Select Case value
                    'Local
                    'Syncing
                    'Synced
                    '[Error]

                    Case 0
                        Return "Using Local DNS"

                    Case 1
                        Return "Trying to sync with Tunlr.net"

                    Case 2
                        Return "Synced with Tunlr.net"

                    Case 3
                        Return "An error occurred during sync"

                    Case Else
                        Return "Unknown State"

                End Select

            Catch ex As Exception
                Return New SolidColorBrush(Colors.LightGray)
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class


    
    Public Class URIToBitmapImageConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Return New BitmapImage(value)
            Catch
                Return Nothing
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class VisiblityToOppositeConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Select Case value
                    Case Visibility.Visible
                        Return Visibility.Collapsed
                    Case Visibility.Hidden
                        Return Visibility.Visible
                    Case Visibility.Collapsed
                        Return Visibility.Visible
                    Case Else
                        Return Visibility.Visible
                End Select
            Catch
                Return Visibility.Visible
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class NothingToVisiblityConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                If parameter Is Nothing Then parameter = False
                Select Case CType(parameter, Boolean)
                    Case False
                        If value Is Nothing Then
                            Return Visibility.Visible
                        Else
                            Return Visibility.Collapsed
                        End If
                    Case True
                        If value Is Nothing Then
                            Return Visibility.Collapsed
                        Else
                            Return Visibility.Visible
                        End If
                End Select
            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class NothingToBoolConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                If parameter Is Nothing Then parameter = False
                Select Case CType(parameter, Boolean)
                    Case False
                        Return value Is Nothing
                    Case True
                        Return Not value Is Nothing
                End Select
            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class BoolToOppositeBoolConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                If targetType <> GetType(Boolean) Then
                    Return False
                End If
                Return Not CBool(value)
            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Try
                If targetType <> GetType(Boolean) Then
                    Return False
                End If
                Return Not CBool(value)
            Catch
                Return value
            End Try
        End Function

#End Region
    End Class

    Public Class BoolToVisiblityConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                'If parameter Is Nothing Then parameter = False
                Select Case CBool(parameter)
                    Case False

                        Select Case CBool(value)
                            Case False
                                Return Visibility.Collapsed
                            Case True
                                Return Visibility.Visible
                        End Select
                    Case True

                        Select Case CBool(value)
                            Case False
                                Return Visibility.Visible
                            Case True
                                Return Visibility.Collapsed
                        End Select
                End Select

            Catch ex As Exception
                Return Visibility.Visible
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class StringFirstUppercaseConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                If value Is Nothing Then Return String.Empty
                If value = String.Empty Then Return String.Empty

                Return Left(value, 1).ToUpper + Right(value, value.ToString.Length - 1).ToLower

            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return value
        End Function

#End Region
    End Class

    Public Class StringAllUppercaseConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Return StrConv(value, VbStrConv.Uppercase)
            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class StringAllLowercaseConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Return StrConv(value, VbStrConv.Lowercase)
            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class StringFirstInWordUppercaseConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value)
            Catch
                Return value
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

#End Region
    End Class

    Public Class MemoryBitsToMegaBytesConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try

                Return Numeric2Bytes(value)
                'Return String.Format("{0:N}", (((value) / 1024) / 1024))
            Catch ex As Exception
                Return 0
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotSupportedException()
        End Function

        Private Function Numeric2Bytes(ByVal b As Double) As String
            Dim byteSize(8) As String
            Dim i As Integer

            byteSize(0) = "Byte"
            byteSize(1) = "KB"
            byteSize(2) = "MB"
            byteSize(3) = "GB"

            b = CDbl(b)
            For i = UBound(byteSize) To 0 Step -1
                If b >= (1024 ^ i) Then
                    Return ThreeNonZeroDigits(b / (1024 ^ i)) & " " & byteSize(i)
                    Exit For
                End If
            Next
        End Function
        Private Function ThreeNonZeroDigits(ByVal value As Double) As String
            If value >= 100 Then
                Return Format$(CInt(value))
            ElseIf value >= 10 Then
                Return Format$(value, "0.0")
            Else
                Return Format$(value, "0.00")
            End If
        End Function

#End Region
    End Class

    Friend Class DatatypeOfDependencyPropertyAdnPropertyConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                If InStr(value.GetType.ToString.ToLower, "propertyinfo".ToLower) Then
                    Return (From Item In TryCast(value, PropertyInfo).ReflectedType.GetProperties Where Item.Name.ToLower = Replace(value.Name.ToLower, "Property".ToLower, "") Select Item).First.PropertyType.Name.ToString
                End If
                If InStr(value.GetType.ToString.ToLower, "FieldInfo".ToLower) Then
                    Return (From Item In TryCast(value, FieldInfo).ReflectedType.GetProperties Where Item.Name.ToLower = Replace(value.Name.ToLower, "Property".ToLower, "") Select Item).First.PropertyType.Name.ToString
                End If


            Catch ex As Exception
                Return Nothing
            End Try

        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack

            Return New SolidColorBrush(value)

        End Function

#End Region
    End Class

    Public Class SolidBrushToColorConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try
                Return TryCast(value, SolidColorBrush).Color
            Catch
                Return Nothing
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack

            Return New SolidColorBrush(value)

        End Function

#End Region
    End Class

    Public Class SecondsToTimeConverter
        Implements IValueConverter
#Region "IValueConverter Members"
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Try

                If value.ToString = "-1" Then Return "∞"

                Dim iSpan As TimeSpan = TimeSpan.FromSeconds(value)
                Dim vreturn As String = If(iSpan.Hours.ToString.PadLeft(2, "0"c) = "00", "", iSpan.Hours.ToString.PadLeft(2, "0"c) & "h ") & _
                        If(iSpan.Minutes.ToString.PadLeft(2, "0"c) = "00", "", iSpan.Minutes.ToString.PadLeft(2, "0"c) & "m ") & _
                        iSpan.Seconds.ToString.PadLeft(2, "0"c) & "s "


                Return vreturn

            Catch
                Return "∞"
            End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack

            Return New SolidColorBrush(value)

        End Function

#End Region
    End Class


End Namespace
